package me.firstapp.manage.utils;

import java.math.BigDecimal;
import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.utils.ShaSign;
import me.firstapp.common.utils.StrUtils;

public class OpenApiUtils {

	private static Logger logger = LoggerFactory.getLogger(OpenApiUtils.class);

	private static final String ACCESS_TOKEN = "3243erttrt565rr3ertfgr5yfy66hgg";
	private static final String ACCESS_SECRET = "132fffrtthh55433grgddw24234";
	private static final String SERVER = "http://172.31.41.250:9158/weixin";

	public static void sendCashApplyMsg(RestTemplate restTemplate, String openId, String cashNo,
			BigDecimal actualCashAmount, String applyTime, Integer status, String shTime, String remark) {
		Calendar calendar = Calendar.getInstance();
		ShaSign shaSign = ShaSign.getSign(ACCESS_TOKEN, ACCESS_SECRET, calendar.getTime());
		StringBuilder url = new StringBuilder(SERVER + "/openApi/messageApi/sendCashApplyMsg.do");
		url.append("?apiKey=").append(ACCESS_TOKEN);
		url.append("&timestamp=").append(calendar.getTimeInMillis());
		url.append("&openId=").append(openId);
		url.append("&cashNo=").append(cashNo);
		url.append("&actualCashAmount=").append(actualCashAmount);
		url.append("&applyTime=").append(applyTime);
		url.append("&status=").append(status);
		url.append("&shTime=").append(shTime);
		url.append("&remark=").append(StrUtils.isNULL(remark) ? "" : remark);

		shaSign.addParam("openId", openId);
		shaSign.addParam("cashNo", cashNo);
		shaSign.addParam("actualCashAmount", actualCashAmount);
		shaSign.addParam("applyTime", applyTime);
		shaSign.addParam("status", status);
		shaSign.addParam("shTime", shTime);
		shaSign.addParam("remark", StrUtils.isNULL(remark) ? "" : remark);
		url.append("&sign=").append(shaSign.signParams());
		ResponseEntity<String> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.POST, null,
				String.class);
		String resultStr = responseEntity.getBody();
		logger.info("调用sendCashApplyMsg.do接口返回结果为：" + resultStr);
		JSONObject jsonObject = JSONObject.parseObject(resultStr);
		String code = jsonObject.getString("code");
		String msg = jsonObject.getString("msg");
		if ("ok".equals(code)) {
			logger.info("调用sendCashApplyMsg.do接口成功");
		} else {
			logger.error("调用sendCashApplyMsg.do接口失败：" + msg);
		}
	}

	public static void sendJinBiChangeMessage(RestTemplate restTemplate, Long jinBiAccountTraceId) {
		Calendar calendar = Calendar.getInstance();
		ShaSign shaSign = ShaSign.getSign(ACCESS_TOKEN, ACCESS_SECRET, calendar.getTime());
		StringBuilder url = new StringBuilder(SERVER + "/openApi/messageApi/sendJinBiChangeMessage.do");
		url.append("?apiKey=").append(ACCESS_TOKEN);
		url.append("&timestamp=").append(calendar.getTimeInMillis());
		url.append("&jinBiAccountTraceId=").append(jinBiAccountTraceId);
		shaSign.addParam("jinBiAccountTraceId", jinBiAccountTraceId);
		url.append("&sign=").append(shaSign.signParams());
		ResponseEntity<String> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.POST, null,
				String.class);
		String resultStr = responseEntity.getBody();
		logger.info("调用sendJinBiChangeMessage.do接口返回结果为：" + resultStr);
		JSONObject jsonObject = JSONObject.parseObject(resultStr);
		String code = jsonObject.getString("code");
		String msg = jsonObject.getString("msg");
		if ("ok".equals(code)) {
			logger.info("调用sendJinBiChangeMessage.do接口成功");
		} else {
			logger.error("调用sendJinBiChangeMessage.do接口失败：" + msg);
		}
	}

	public static void sendWeiBiChangeMessage(RestTemplate restTemplate, Long weiBiAccountTraceId) {
		Calendar calendar = Calendar.getInstance();
		ShaSign shaSign = ShaSign.getSign(ACCESS_TOKEN, ACCESS_SECRET, calendar.getTime());
		StringBuilder url = new StringBuilder(SERVER + "/openApi/messageApi/sendWeiBiChangeMessage.do");
		url.append("?apiKey=").append(ACCESS_TOKEN);
		url.append("&timestamp=").append(calendar.getTimeInMillis());
		url.append("&weiBiAccountTraceId=").append(weiBiAccountTraceId);
		shaSign.addParam("weiBiAccountTraceId", weiBiAccountTraceId);
		url.append("&sign=").append(shaSign.signParams());
		ResponseEntity<String> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.POST, null,
				String.class);
		String resultStr = responseEntity.getBody();
		logger.info("调用sendWeiBiChangeMessage.do接口返回结果为：" + resultStr);
		JSONObject jsonObject = JSONObject.parseObject(resultStr);
		String code = jsonObject.getString("code");
		String msg = jsonObject.getString("msg");
		if ("ok".equals(code)) {
			logger.info("调用sendWeiBiChangeMessage.do接口成功");
		} else {
			logger.error("调用sendWeiBiChangeMessage.do接口失败：" + msg);
		}
	}

	public static void sendOrderMsg(RestTemplate restTemplate, String orderNo) {
		Calendar calendar = Calendar.getInstance();
		ShaSign shaSign = ShaSign.getSign(ACCESS_TOKEN, ACCESS_SECRET, calendar.getTime());
		StringBuilder url = new StringBuilder(SERVER + "/openApi/messageApi/sendOrderMsg.do");
		url.append("?apiKey=").append(ACCESS_TOKEN);
		url.append("&timestamp=").append(calendar.getTimeInMillis());
		url.append("&orderNo=").append(orderNo);
		shaSign.addParam("orderNo", orderNo);
		url.append("&sign=").append(shaSign.signParams());
		ResponseEntity<String> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.POST, null,
				String.class);
		String resultStr = responseEntity.getBody();
		logger.info("调用sendOrderMsg.do接口返回结果为：" + resultStr);
		JSONObject jsonObject = JSONObject.parseObject(resultStr);
		String code = jsonObject.getString("code");
		String msg = jsonObject.getString("msg");
		if ("ok".equals(code)) {
			logger.info("调用sendOrderMsg.do接口成功");
		} else {
			logger.error("调用sendOrderMsg.do接口失败：" + msg);
		}
	}

	public static void returnOrderMsg(RestTemplate restTemplate, String orderNo) {
		Calendar calendar = Calendar.getInstance();
		ShaSign shaSign = ShaSign.getSign(ACCESS_TOKEN, ACCESS_SECRET, calendar.getTime());
		StringBuilder url = new StringBuilder(SERVER + "/openApi/messageApi/returnOrderMsg.do");
		url.append("?apiKey=").append(ACCESS_TOKEN);
		url.append("&timestamp=").append(calendar.getTimeInMillis());
		url.append("&orderNo=").append(orderNo);
		shaSign.addParam("orderNo", orderNo);
		url.append("&sign=").append(shaSign.signParams());
		ResponseEntity<String> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.POST, null,
				String.class);
		String resultStr = responseEntity.getBody();
		logger.info("调用returnOrderMsg.do接口返回结果为：" + resultStr);
		JSONObject jsonObject = JSONObject.parseObject(resultStr);
		String code = jsonObject.getString("code");
		String msg = jsonObject.getString("msg");
		if ("ok".equals(code)) {
			logger.info("调用returnOrderMsg.do接口成功");
		} else {
			logger.error("调用returnOrderMsg.do接口失败：" + msg);
		}
	}
}
